package com.fzx.testprinter;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import net.posprinter.posprinterface.IMyBinder;
import net.posprinter.posprinterface.ProcessData;
import net.posprinter.posprinterface.TaskCallback;
import net.posprinter.service.PosprinterService;
import net.posprinter.utils.BitmapProcess;
import net.posprinter.utils.BitmapToByteData;
import net.posprinter.utils.DataForSendToPrinterPos80;
import net.posprinter.utils.PosPrinterDev;
import net.posprinter.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    public IMyBinder myBinder;
    private List<String> usbList;
    private boolean isConnect = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindService();
    }

    private void bindService() {
        Intent intent = new Intent(this, PosprinterService.class);
        bindService(intent, mSerconnection, BIND_AUTO_CREATE);
    }


    ServiceConnection mSerconnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (IMyBinder) service;
            Log.i(TAG, "onServiceConnected: ");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "onServiceDisconnected: ");
        }
    };

    private void connectUsb() {
        usbList = PosPrinterDev.GetUsbPathNames(this);
        String dev = "";
        if (usbList != null && usbList.size() > 0) {
            dev = usbList.get(0);
            if (dev != null && !"".equals(dev)) {
                myBinder.ConnectUsbPort(getApplicationContext(), dev, new TaskCallback() {
                    @Override
                    public void OnSucceed() {
                        isConnect = true;
                        Log.i(TAG, "OnSucceed: 连接成功");
                        Toast.makeText(getApplicationContext(), "连接成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void OnFailed() {
                        isConnect = false;
                        Log.i(TAG, "OnFailed: 连接失败");
                        Toast.makeText(getApplicationContext(), "连接失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    }

    private void disConnect() {
        if (isConnect) {
            myBinder.DisconnectCurrentPort(new TaskCallback() {
                @Override
                public void OnSucceed() {
                    isConnect = false;
                    Toast.makeText(getApplicationContext(), "断开连接成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void OnFailed() {
                    isConnect = true;
                    Toast.makeText(getApplicationContext(), "断开连接失败", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }


    private void printSample() {
        if (isConnect) {
            myBinder.WriteSendData(new TaskCallback() {
                @Override
                public void OnSucceed() {
                    Toast.makeText(getApplicationContext(), "连接成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void OnFailed() {
                    Toast.makeText(getApplicationContext(), "连接失败", Toast.LENGTH_SHORT).show();
                }
            }, new ProcessData() {
                @Override
                public List<byte[]> processDataBeforeSend() {
                    List<byte[]> list = new ArrayList<>();
                    list.add(DataForSendToPrinterPos80.initializePrinter());//初始化
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(60, 00));//设置初始位置
                    list.add(DataForSendToPrinterPos80.selectCharacterSize(17));//字体放大一倍
                    list.add(StringUtils.strTobytes("商品"));
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(100, 01));
                    list.add(StringUtils.strTobytes("价格"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());//打印并换行
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(60, 00));
                    list.add(StringUtils.strTobytes("黄焖鸡"));
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(100, 01));
                    list.add(StringUtils.strTobytes("5元"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(60, 00));
                    list.add(StringUtils.strTobytes("黄焖鸡呀"));
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(100, 01));
                    list.add(StringUtils.strTobytes("6元"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(60, 00));
                    list.add(StringUtils.strTobytes("黄焖鸡"));
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(100, 01));
                    list.add(StringUtils.strTobytes("7元"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());//打印走纸

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(60, 00));
                    list.add(StringUtils.strTobytes("黄焖鸡"));
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(100, 01));
                    list.add(StringUtils.strTobytes("8元"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(60, 00));
                    list.add(StringUtils.strTobytes("黄焖鸡"));
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(100, 01));
                    list.add(StringUtils.strTobytes("9元"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(60, 00));
                    list.add(StringUtils.strTobytes("黄焖鸡"));
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(100, 01));
                    list.add(StringUtils.strTobytes("10元"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    return list;
                }
            });
        } else {
            Toast.makeText(this, "打印机未连接", Toast.LENGTH_SHORT).show();
        }
    }

    private void printText() {

        if (isConnect) {
            myBinder.WriteSendData(new TaskCallback() {
                @Override
                public void OnSucceed() {
                    Toast.makeText(getApplicationContext(), "连接成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void OnFailed() {
                    Toast.makeText(getApplicationContext(), "连接失败", Toast.LENGTH_SHORT).show();
                }
            }, new ProcessData() {
                @Override
                public List<byte[]> processDataBeforeSend() {
                    List<byte[]> list = new ArrayList<>();
                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(StringUtils.strTobytes("1234567890qwertyuiopakjbdscm nkjdv mcdskjb"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    return list;
                }
            });
        } else {
            Toast.makeText(this, "打印机未连接", Toast.LENGTH_SHORT).show();
        }

    }

    public void OnBack(View view) {
        finish();
    }

    public void OnConnectUSB(View view) {
        connectUsb();
    }

    public void OnDisconnectUSB(View view) {
        disConnect();
    }

    public void OnPrintSample(View view) {
        printSample();
    }

    public void OnPrintText(View view) {
        print();
    }

    public void OnPrintBarcode(View view) {

    }

    public void OnPrintQRCode(View view) {
        printQRCode();
    }

    public void OnPrintBitmap(View view) {
        printBmp();
    }

    private Bitmap bitmap1;
    private Bitmap bitmap2;
    private void print(){
        if (isConnect) {
            bitmap1 = BitmapFactory.decodeResource(getResources(),R.mipmap.img_print_bg);
            bitmap2 = BitmapFactory.decodeResource(getResources(),R.mipmap.img_print_null);
            myBinder.WriteSendData(new TaskCallback() {
                @Override
                public void OnSucceed() {
                    Toast.makeText(getApplicationContext(), "连接成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void OnFailed() {
                    Toast.makeText(getApplicationContext(), "连接失败", Toast.LENGTH_SHORT).show();
                }
            }, new ProcessData() {
                @Override
                public List<byte[]> processDataBeforeSend() {
                    List<byte[]> list = new ArrayList<>();
                    //初始化
                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));//左中右对齐 0,1,2
                    list.add(DataForSendToPrinterPos80.selectOrCancelBoldModel(1));
                    //n的0到3位设定字符高度，4-7位用来设定字符宽度
                    //1111 1111 ，0，17，34，
                    list.add(DataForSendToPrinterPos80.selectCharacterSize(17));//字体放大一倍
                    list.add(StringUtils.strTobytes("广汽本田 2019杭州马拉松"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(StringUtils.strTobytes("guangqi 2019hangzhoumalasong"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(StringUtils.strTobytes("参赛号"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(DataForSendToPrinterPos80.selectOrCancelBoldModel(1));//0不加粗，1加粗
                    list.add(DataForSendToPrinterPos80.selectCharacterSize(17));
                    list.add(StringUtils.strTobytes("A1200001"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(StringUtils.strTobytes("打印时间 2019/11/21 11:27:31"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(StringUtils.strTobytes("技术支持：蜂之舞芯片计时"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(StringUtils.strTobytes("------------------------------"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(80,00));
                    list.add(StringUtils.strTobytes("参赛号：A1200001"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(80,00));
                    list.add(StringUtils.strTobytes("姓名：张三"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(80,00));
                    list.add(StringUtils.strTobytes("性别：男"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.setAbsolutePrintPosition(80,00));
                    list.add(StringUtils.strTobytes("证件号：3506123412341233232"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(StringUtils.strTobytes("参赛包+参数服领取"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    //图片
                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    List<Bitmap> blist1= new ArrayList<>();
                    blist1 = BitmapProcess.cutBitmap(150,bitmap1);
                    for (int i= 0 ;i<blist1.size();i++){
                        list.add(DataForSendToPrinterPos80.printRasterBmp(0,blist1.get(i), BitmapToByteData.BmpType.Dithering, BitmapToByteData.AlignType.Center,576));
                    }
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(StringUtils.strTobytes("请撕下此联！以免影响物资领取"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(StringUtils.strTobytes("Please don't tear this down"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(StringUtils.strTobytes("------------------------------"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(StringUtils.strTobytes("号码簿芯片领取窗口"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(StringUtils.strTobytes("Bib Number&Chips Counter"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(DataForSendToPrinterPos80.selectOrCancelBoldModel(1));//0不加粗，1加粗
                    list.add(DataForSendToPrinterPos80.selectCharacterSize(34));
                    list.add(StringUtils.strTobytes("14"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());

                    //图片
                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    List<Bitmap> blist2= new ArrayList<>();
                    blist2 = BitmapProcess.cutBitmap(150,bitmap1);
                    for (int i= 0 ;i<blist2.size();i++){
                        list.add(DataForSendToPrinterPos80.printRasterBmp(0,blist2.get(i), BitmapToByteData.BmpType.Dithering, BitmapToByteData.AlignType.Center,576));
                    }
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());


                    return list;
                }
            });
        } else {
            Toast.makeText(this, "打印机未连接", Toast.LENGTH_SHORT).show();
        }
    }

    private void printBmp(){
        if (isConnect) {
            bitmap1 = BitmapFactory.decodeResource(getResources(),R.mipmap.img_xl);
            bitmap2 = BitmapFactory.decodeResource(getResources(),R.mipmap.img_print_null);
            myBinder.WriteSendData(new TaskCallback() {
                @Override
                public void OnSucceed() {
                    Toast.makeText(getApplicationContext(), "连接成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void OnFailed() {
                    Toast.makeText(getApplicationContext(), "连接失败", Toast.LENGTH_SHORT).show();
                }
            }, new ProcessData() {
                @Override
                public List<byte[]> processDataBeforeSend() {
                    List<byte[]> list = new ArrayList<>();
                    list.add(DataForSendToPrinterPos80.initializePrinter());
//                    list.add(DataForSendToPrinterPos80.selectAlignment(0));
                    list.add(StringUtils.strTobytes("上海蜂之舞"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    List<Bitmap> blist1= new ArrayList<>();
                    blist1 = BitmapProcess.cutBitmap(150,bitmap1);
                    for (int i= 0 ;i<blist1.size();i++){
                        list.add(DataForSendToPrinterPos80.printRasterBmp(0,blist1.get(i), BitmapToByteData.BmpType.Dithering, BitmapToByteData.AlignType.Center,576));
                    }


                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    return list;
                }
            });
        } else {
            Toast.makeText(this, "打印机未连接", Toast.LENGTH_SHORT).show();
        }
    }

    private void printQRCode(){
        if (isConnect) {
            myBinder.WriteSendData(new TaskCallback() {
                @Override
                public void OnSucceed() {
                    Toast.makeText(getApplicationContext(), "连接成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void OnFailed() {
                    Toast.makeText(getApplicationContext(), "连接失败", Toast.LENGTH_SHORT).show();
                }
            }, new ProcessData() {
                @Override
                public List<byte[]> processDataBeforeSend() {
                    List<byte[]> list = new ArrayList<>();
                    list.add(DataForSendToPrinterPos80.initializePrinter());
                    list.add(DataForSendToPrinterPos80.selectAlignment(1));
                    list.add(DataForSendToPrinterPos80.printQRcode(3,48,"www.xprinter.net"));
                    list.add(DataForSendToPrinterPos80.printAndFeedLine());
                    return list;
                }
            });
        } else {
            Toast.makeText(this, "打印机未连接", Toast.LENGTH_SHORT).show();
        }
    }


}
